//============================================================================================
/**
 * @file	phclink_obj.c
 * @brief	PHC画面OBJ処理
 * @author	AkitoMori
 * @date	09.01.29
 */
//============================================================================================
#include "common.h"
#include "gflib/touchpanel.h"
#include "system/gamedata.h"
#include "system/procsys.h"
#include "system/fontproc.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/window.h"
#include "system/pm_str.h"
#include "system/lib_pack.h"
#include "system/snd_tool.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/brightness.h"
#include "system/buflen.h"
#include "system/msgdata.h"
#include "system/wordset.h"
#include "system/numfont.h"
#include "poketool/pokeicon.h"
#include "savedata/config.h"
#include "itemtool/item.h"
#include "battle/wazatype_icon.h"
#include "battle/battle_common.h"
#include "poketool/waza_tool.h"

#include "application/phclink.h"
#include "phclink_local.h"
#include "phclink_obj.h"
#include "phc_ds.naix"


//============================================================================================
//	定数定義
//============================================================================================
enum {
	PCMV_SEQ_WAIT = 0,
	PCMV_SEQ_MOVE,
};

#define	BMP_P_ICON_PX	( 0 )
#define	BMP_P_ICON_PY	( 11 )
#define	BMP_P_ICON_SX	( 12 )
#define	BMP_P_ICON_SY	( 2 )


#define	CLACT_ID_COMMON		( 49702 )	///< この画面で使用するセルアクターのID

// キャラリソースID
enum {
	CHR_ID_SUB_PANEL = CLACT_ID_COMMON,	///< サブパネル
	CHR_ID_MAIN_PANEL,					///< メインパネル
	CHR_ID_ITEMICON0,					///< アイテムアイコン
	CHR_ID_ITEMICON1,					///< アイテムアイコン
	CHR_ID_ITEMICON2,					///< アイテムアイコン
	CHR_ID_ITEMICON3,					///< アイテムアイコン
	CHR_ID_ITEMICON4,					///< アイテムアイコン
	CHR_ID_ITEMICON5,					///< アイテムアイコン
	CHR_ID_TYPEICON,					///< タイプアイコン
	CHR_ID_KINDICON,					///< 分類アイコン

	CHR_ID_MAX = CHR_ID_KINDICON - CLACT_ID_COMMON + 1
};
// パレットリソースID
enum {
	PAL_ID_SUB_PANEL = CLACT_ID_COMMON,	///< 上画面
	PAL_ID_MAIN_PANEL,					///< 下画面

	PAL_ID_MAX = PAL_ID_MAIN_PANEL - CLACT_ID_COMMON + 1
};
// セルリソースID
enum {
	CEL_ID_SUB_PANEL  = CLACT_ID_COMMON,	///< 上画面
	CEL_ID_MAIN_PANEL,						///< 下画面

	CEL_ID_MAX = CEL_ID_MAIN_PANEL - CLACT_ID_COMMON + 1
};
// セルアニメリソースID
enum {
	ANM_ID_SUB_PANEL = CLACT_ID_COMMON,	///< 上画面
	ANM_ID_MAIN_PANEL,					///< 下画面

	ANM_ID_MAX = ANM_ID_MAIN_PANEL - CLACT_ID_COMMON + 1
};


///< PHC画面の上下画面のドット距離
#define 	PHCLINK_SUB_ACTOR_DISTANCE	( 256*FX32_ONE )

///< ポケモン正面絵のパレットが格納されている列
#define SUB_POKE_GRA_PAL	( 1 )
#define MAIN_POKE_GRA_PAL	( 4 )

///< ポケモンアイコンのパレットが格納される列（これ以降３列使用）
#define MAIN_POKEICON_PAL	( 5 )

//============================================================================================
//	プロトタイプ宣言
//============================================================================================
static void ClactResManInit( PHCLINK_WORK * wk );
static void ClactGraphicLoad( PHCLINK_WORK * wk );
static void ClactAdd( PHCLINK_WORK * wk );

static void PocketCursorMove( PHCLINK_WORK * wk );

static void pokeicon_pal_trans( void );
static void TransPokeIconCharaPal( NNSG2dCharacterData *chara, int pokeno, int form, int tamago, int no, CATS_ACT_PTR icon);
static int _light_obj_search( PHC_LIGHT_WORK *lights );


#define LIGHT_OAM_MAIN	( 15)	///< メイン画面用光OBJの定義
#define LIGHT_OAM_SUB	( 16 )	///< サブ画面用光OBJの定義

//============================================================================================
//	グローバル変数
//============================================================================================
// セルアクターデータ
static const TCATS_OBJECT_ADD_PARAM_S ClactParamTbl[] =
{
	{	// カーソル
		22,59, 0,
		8, 0, 1+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		1, 0
	},
	{	// PHC本体画像
		128,59, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB_PANEL, PAL_ID_SUB_PANEL, CEL_ID_SUB_PANEL, ANM_ID_SUB_PANEL, 0, 0 },
		1, 0
	},
	{	// PHC本体画像
		128,59, 0,
		2, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB_PANEL, PAL_ID_SUB_PANEL, CEL_ID_SUB_PANEL, ANM_ID_SUB_PANEL, 0, 0 },
		1, 0
	},
	{	// 左矢印
		17,173, 0,
		0, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		1, 0
	},
	{	// 右矢印
		55,173, 0,
		1, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		1, 0
	},
	{	// モンスターボール							5
		50,128, 0,
		6, 0, 2+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		1, 0
	},
	{	// 宝箱
		128,100, 0,
		7, 0, 3+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		1, 0
	},

	{	// ポケモン正面絵（上画面）
		64, 54, 0,
		3, 0, SUB_POKE_GRA_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB_PANEL, PAL_ID_SUB_PANEL, CEL_ID_SUB_PANEL, ANM_ID_SUB_PANEL, 0, 0 },
		0, 0
	},
	{	// ポケモン正面絵（下画面）
		128, 100, 0,
		14, 0, MAIN_POKE_GRA_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},
	{	// ポケモンアイコン0
		168-32, 112, 0,
		15, 0, MAIN_POKEICON_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},
	{	// ポケモンアイコン1					10
		168-32, 112, 0,
		16, 0, MAIN_POKEICON_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},
	{	// ポケモンアイコン2
		168-32, 112, 0,
		17, 0, MAIN_POKEICON_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},
	{	// ポケモンアイコン3
		168-32, 112, 0,
		18, 0, MAIN_POKEICON_PAL+TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},

	{	// 上っていく光のＯＢＪ（下画面）
		128, 112, 0,
		24, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},
	{	// 上っていく光のＯＢＪ（上画面）
		128, 112, 0,
		10, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB_PANEL, PAL_ID_SUB_PANEL, CEL_ID_SUB_PANEL, ANM_ID_SUB_PANEL, 0, 0 },
		0, 0
	},

	{	// 光のOBJメインLCD用					15
		128, 112, 0,
		19, 0, 0, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN_PANEL, PAL_ID_MAIN_PANEL, CEL_ID_MAIN_PANEL, ANM_ID_MAIN_PANEL, 0, 0 },
		0, 0
	},

	{	// 光のOBJサブLCD用
		128, 112, 0,
		4, 0, 0, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB_PANEL, PAL_ID_SUB_PANEL, CEL_ID_SUB_PANEL, ANM_ID_SUB_PANEL, 0, 0 },
		0, 0
	},

};



//--------------------------------------------------------------------------------------------
/**
 * @brief	セルアクター初期化
 *
 * @param	wk		バッグ画面のワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void PhcLinkObj_Init( PHCLINK_WORK * wk )
{
	GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// MAIN DISP OBJ ON
	GF_Disp_GXS_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );		// SUB DISP OBJ ON

	initVramTransferManagerHeap( 64, HEAPID_PHC_LINK );

	ClactResManInit( wk );
	ClactGraphicLoad( wk );
	ClactAdd( wk );

	{
		CLACT_U_EASYRENDER_DATA *renddata = CATS_EasyRenderGet( wk->csp );
		CLACT_U_SetSubSurfaceMatrix( renddata, 0, PHCLINK_SUB_ACTOR_DISTANCE );
	}

	// ポケモンアイコンパレット転送
	pokeicon_pal_trans();

}

//--------------------------------------------------------------------------------------------
/**
 * @brief	 セルアクター解放
 *
 * @param	wk		ワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void PhcLinkObj_ClactFree( PHCLINK_WORK * wk )
{
	u32	i;

	for( i=0; i<PHCLINK_ALL_OBJ_MAX; i++ ){
		CATS_ActorPointerDelete_S( wk->cap[i] );
	}
	CATS_ResourceDestructor_S( wk->csp, wk->crp );
	CATS_FreeMemory( wk->csp );

}


//--------------------------------------------------------------------------------------------
/**
 * @brief	 アイテムアイコン変更
 *
 * @param	wk		バッグ画面のワーク
 * @param	item	アイテム番号
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void PhcLinkObj_PokemonIconChange( PHCLINK_WORK * wk, int pos, u16 no, u8 form )
{
/*
	CATS_ChangeResourceCharArc(
		wk->csp, wk->crp,
		ARC_ITEMICON, GetItemIndex( item, ITEM_GET_ICON_CGX ), 0, CHR_ID_ITEMICON0+pos );
	CATS_ChangeResourcePlttArc(
		wk->csp, wk->crp,
		ARC_ITEMICON, GetItemIndex( item, ITEM_GET_ICON_PAL ), 0, PAL_ID_ITEMICON0+pos );
*/
}







//--------------------------------------------------------------------------------------------
/**
 * @brief	 リソースマネージャー初期化
 *
 * @param	wk		ワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactResManInit( PHCLINK_WORK * wk )
{
	TCATS_RESOURCE_NUM_LIST	crnl = { CHR_ID_MAX, PAL_ID_MAX, CEL_ID_MAX, ANM_ID_MAX, 0, 0 };

	wk->csp = CATS_AllocMemory( HEAPID_PHC_LINK );
	wk->crp = CATS_ResourceCreate( wk->csp );
	{
		TCATS_OAM_INIT	coi = {
			0, 128,
			0, 32,
			0, 128,
			0, 32,
		};
		TCATS_CHAR_MANAGER_MAKE ccmm = {
			PHCLINK_OBJ_MAX,
			1024*64,
			1024*16,
			GX_OBJVRAMMODE_CHAR_1D_32K,
			GX_OBJVRAMMODE_CHAR_1D_32K
		};
		CATS_SystemInit( wk->csp, &coi, &ccmm, 32 );
	}
	CATS_ClactSetInit( wk->csp, wk->crp, PHCLINK_ALL_OBJ_MAX );
	CATS_ResourceManagerInit( wk->csp, wk->crp, &crnl );
}

//--------------------------------------------------------------------------------------------
/**
 * @brief	 セルアクターのグラフィックロード
 *
 * @param	wk		バッグ画面のワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactGraphicLoad( PHCLINK_WORK * wk )
{
	ARCHANDLE *handle = ArchiveDataHandleOpen( ARC_PHCLINK_GRA, HEAPID_PHC_LINK );


	// ------------ サブパネル（上画面） ------------------------
	// -------キャラ転送ｰｰｰｰｰｰｰｰｰ
	CATS_LoadResourceCharArcH( wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_u_lz_ncgr, 1, NNS_G2D_VRAM_TYPE_2DSUB,
							   CHR_ID_SUB_PANEL );
	// -----------パレット-------------
	// サブパネル用
	CATS_LoadResourcePlttArcH( wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_u_nclr, 0, 2, NNS_G2D_VRAM_TYPE_2DSUB,
							   PAL_ID_SUB_PANEL );


	// ----------セル----------------
	// サブパネル用セルデータ
	CATS_LoadResourceCellArcH(  wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_u_ncer, 0,
							   CEL_ID_SUB_PANEL );

	// -------------セルアニメ---------------
	// サブパネル用アニメ
	CATS_LoadResourceCellAnmArcH( wk->csp, wk->crp, handle,
								 NARC_phc_ds_phcds_obj_u_nanr, 0,
								 ANM_ID_SUB_PANEL );


	// ------------ メインパネル（下画面） ------------------------
	// -------キャラ転送ｰｰｰｰｰｰｰｰｰ
	CATS_LoadResourceCharArcH( wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_d_lz_ncgr, 1, NNS_G2D_VRAM_TYPE_2DMAIN,
							   CHR_ID_MAIN_PANEL );
	// -----------パレット-------------
	// サブパネル用
	CATS_LoadResourcePlttArcH( wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_s_nclr, 0, 7, NNS_G2D_VRAM_TYPE_2DMAIN,
							   PAL_ID_MAIN_PANEL );


	// ----------セル----------------
	// サブパネル用セルデータ
	CATS_LoadResourceCellArcH(  wk->csp, wk->crp, handle,
							   NARC_phc_ds_phcds_obj_d_ncer, 0,
							   CEL_ID_MAIN_PANEL );

	// -------------セルアニメ---------------
	// サブパネル用アニメ
	CATS_LoadResourceCellAnmArcH( wk->csp, wk->crp, handle,
								 NARC_phc_ds_phcds_obj_d_nanr, 0,
								 ANM_ID_MAIN_PANEL );



	ArchiveDataHandleClose( handle );
}

#define BAG_SELECT_PLATE_NUM	( 4 )

static const u8 act_pal_tbl[]={
		1,
		0,
		0,
		0,
		0,
		2,
		3,
		SUB_POKE_GRA_PAL,
		MAIN_POKE_GRA_PAL,
		MAIN_POKEICON_PAL,
		MAIN_POKEICON_PAL,
		MAIN_POKEICON_PAL,
		MAIN_POKEICON_PAL,
};

//--------------------------------------------------------------------------------------------
/**
 * @brief	 バッグ画面のセルアクターをセット
 *
 * @param	wk		ワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void ClactAdd( PHCLINK_WORK * wk )
{
	u32	i;

	// 通常OBJ登録
	for( i=0; i<PHCLINK_OBJ_MAX; i++ ){
		wk->cap[i] = CATS_ObjectAdd_S_SubDistance( wk->csp, wk->crp, &ClactParamTbl[i], PHCLINK_SUB_ACTOR_DISTANCE );

		// パレット指定
		CATS_ObjectPaletteSetCap( wk->cap[i], act_pal_tbl[i]);

		// 隠す
		CATS_ObjectEnableCap( wk->cap[ i ], ACT_OFF );

	}

	CATS_ObjectBGPriSetCap( wk->cap[ PLOBJ_ID_PHC ], 2 );

	// 光50個登録（メイン画面）
	for(;i<PHCLINK_ALL_OBJ_MAX-PHC_LIGHT_MAX;i++){
		wk->cap[i] = CATS_ObjectAdd_S( wk->csp, wk->crp, &ClactParamTbl[LIGHT_OAM_MAIN] );
		// パレット指定
		CATS_ObjectPaletteSetCap( wk->cap[i], 0);
		CATS_ObjectEnableCap( wk->cap[ i ], ACT_OFF );
		CATS_ObjectBGPriSetCap( wk->cap[ i ], 2 );
	}

	// 光もう50個登録（サブ画面）
	for(;i<PHCLINK_ALL_OBJ_MAX;i++){
		wk->cap[i] = CATS_ObjectAdd_S_SubDistance( wk->csp, wk->crp, &ClactParamTbl[LIGHT_OAM_SUB], PHCLINK_SUB_ACTOR_DISTANCE );
		// パレット指定
		CATS_ObjectPaletteSetCap( wk->cap[i], 0);
		CATS_ObjectEnableCap( wk->cap[ i ], ACT_OFF );
		CATS_ObjectBGPriSetCap( wk->cap[ i ], 2 );
	}

	// 光OBJ管理ワーク初期化
	PhcLink_LightObjInit(wk);

	// ポケモン正面絵OBJのプライオリティ＆アフィン設定
	CATS_ObjectPriSetCap( wk->cap[PLOBJ_ID_POKEMON_D], 2);
	CATS_ObjectAffineSetCap( wk->cap[PLOBJ_ID_POKEMON_D],CLACT_AFFINE_NORMAL );

	// ページ切り替え矢印OBJアニメ設定
	CATS_ObjectAutoAnimeSetCap(  wk->cap[PLOBJ_ID_ARROW_L], CATS_ANM_AUTO_ON);
	CATS_ObjectAutoAnimeSetCap(  wk->cap[PLOBJ_ID_ARROW_R], CATS_ANM_AUTO_ON);


}



typedef struct{
	u8 x,y;
	u8 anim;
	u8 pal;
}PHCLINK_CURSOR_INFO;

static const PHCLINK_CURSOR_INFO phclink_cursor_tbl[]={
	{128, 64-8, 8, 1,},	// ２択の上
	{128,103-8, 8, 1,},	// ２択の下
	{128, 88-8, 8, 1,},	// 選択肢一つ
	{224,176,10, 1,},	// もどる
	{152,176, 9, 1,},	// けってい
	{16+32*2,16,8,9,},
	{128,  16,   8, 1,},	// ５択の０
	{128,  48,	 8, 1,},	// ５択の１
	{128,  80,   8, 1,},	// ５択の２
	{128, 112,  8, 1,},	// ５択の３
	{128, 144,  8, 1,},	// ５択の４
	{128,  32, 8, 1,},	// ３択の上
	{128,  72, 8, 1,},	// ３択の中
	{128, 112, 8, 1,},	// ３択の下


};




//=============================================================================================
/**
 * @brief	 カーソル初期化
 *
 * @param   wk		PHC画面ワーク
 */
//=============================================================================================
void PhcLinkObj_CursorInit(  PHCLINK_WORK *wk )
{

	CATS_ObjectAutoAnimeSetCap(  wk->cap[PLOBJ_ID_CURSOR], CATS_ANM_AUTO_ON);
	CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_CURSOR], phclink_cursor_tbl[0].anim );
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_CURSOR ], ACT_OFF );
}

//==============================================================================
/**
 * @brief	カーソルの位置＆形状制御
 *
 * @param   wk		PHC画面ワーク
 * @param   pos		カーソル表示の指定番号
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_CursorPosAppearSet( PHCLINK_WORK *wk, int pos )
{

	CATS_ObjectPosSetCap_SubSurface( wk->cap[PLOBJ_ID_CURSOR],
							phclink_cursor_tbl[pos].x,
							phclink_cursor_tbl[pos].y,
							PHCLINK_SUB_ACTOR_DISTANCE );
	CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_CURSOR], phclink_cursor_tbl[pos].anim );
	CATS_ObjectPaletteSetCap( wk->cap[PLOBJ_ID_CURSOR], phclink_cursor_tbl[pos].pal );
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_CURSOR ], ACT_ON );

}

//==============================================================================
/**
 * @brief   カーソル隠す
 *
 * @param   wk		PHC画面ワーク
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_CursorDisappear( PHCLINK_WORK *wk )
{
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_CURSOR ], ACT_OFF );

}


//=============================================================================================
/**
 * @brief	 カーソルを点滅させる
 *
 * @param   wk		PHC画面ワーク
 */
//=============================================================================================
void PhcLinkObj_CursorBlink( PHCLINK_WORK *wk )
{
	int anime = CATS_ObjectAnimeSeqGetCap( wk->cap[ PLOBJ_ID_CURSOR ] );

	// カーソル形状の３つ先に同じ形状の点滅アニメ版が存在している
	CATS_ObjectAnimeSeqSetCap( wk->cap[ PLOBJ_ID_CURSOR ], anime+3);

}


//==============================================================================
/**
 * @brief   PHC本体出す
 *
 * @param   wk		PHC画面ワーク
 * @param   anim	アニメ指定
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_PhcAppear( PHCLINK_WORK *wk, int anim )
{
	// PHCとアニメを表示ON
	CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_PHC], 0 );
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC ], ACT_ON );

	if(anim==0){
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC_ANIME ], ACT_OFF );
	}else if(anim>0){
		CATS_ObjectPriSetCap( wk->cap[ PLOBJ_ID_PHC ], 2 );
		CATS_ObjectPriSetCap( wk->cap[ PLOBJ_ID_PHC_ANIME ], 1 );
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC_ANIME ], ACT_ON );
		CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_PHC_ANIME], anim );
		CATS_ObjectAutoAnimeSetCap( wk->cap[PLOBJ_ID_PHC_ANIME], 1);
	}
}


#define PHC_NOW_CONNECT			( 1 )
#define PHC_STANDBY_CONNECT		( 2 )

//=============================================================================================
/**
 * @brief	 赤外線接続状況でアニメーションを変える
 *
 * @param   wk		PHC画面ワーク
 */
//=============================================================================================
void PhcLinkObj_PhcIrcAnime( PHCLINK_WORK *wk )
{
	CATS_ACT_PTR phc_anime = wk->cap[PLOBJ_ID_PHC_ANIME];

	// 赤外線が繋がっている
	if(PHC_IsConnect()){
		if(CATS_ObjectAnimeSeqGetCap(phc_anime)==PHC_STANDBY_CONNECT){
			CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_PHC_ANIME], PHC_NOW_CONNECT );
		}
	// 赤外線が繋がっていない
	}else{
		if(CATS_ObjectAnimeSeqGetCap(phc_anime)==PHC_NOW_CONNECT){
			CATS_ObjectAnimeSeqSetCap( wk->cap[PLOBJ_ID_PHC_ANIME], PHC_STANDBY_CONNECT );
		}

	}
}

//==============================================================================
/**
 * @brief   PHC本体出す
 *
 * @param   wk		PHC画面ワーク
 * @param   anim	アニメ番号
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_PhcDisappear( PHCLINK_WORK *wk )
{
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC ], ACT_OFF );
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC_ANIME ], ACT_OFF );

}


//==============================================================================
/**
 * @brief   指定のセルアクターのON
 *
 * @param   wk		PHC画面ワーク
 * @param   no		セルアクター指定番号
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_Appear( PHCLINK_WORK *wk, int no )
{
	CATS_ObjectEnableCap( wk->cap[ no ], ACT_ON );		// ON
}
//=============================================================================================
/**
 * @brief	指定のセルアクターのOFF
 *
 * @param   wk	PHC画面ワーク
 * @param   no	セルアクター指定番号
 */
//=============================================================================================
void PhcLinkObj_Disappear( PHCLINK_WORK *wk, int no )
{
	CATS_ObjectEnableCap( wk->cap[ no ], ACT_OFF );		// OFF
}


//==============================================================================
/**
 * @brief   指定のセルアクターのアニメ切り替え
 *
 * @param   wk		PHC画面ワーク
 * @param   no		セルアクターINDEX
 * @param   anim	アニメINDEX
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_AnimSet( PHCLINK_WORK *wk, int no, int anim )
{
	CATS_ObjectAnimeSeqSetCap( wk->cap[no], anim );
}


//==============================================================================
/**
 * @brief   PHC用セルアクターの座標設定
 *
 * @param   wk		PHC画面ワーク
 * @param   no		セルアクターINDEX
 * @param   x		X座標
 * @param   y		Y座標
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_PosSet( PHCLINK_WORK *wk, int no, int x, int y )
{
	CATS_ObjectPosSetCap_SubSurface( wk->cap[no],
							x, 	y,	PHCLINK_SUB_ACTOR_DISTANCE );
}


//==============================================================================
/**
 * @brief   メイン用とサブ用の２つのOBJを一つのワークで管理して行き来できるようにする
 *
 * @param   wk			PHC画面ワーク
 * @param   lightno		制御するOAMのNO,作成時は50個まで
 * @param   pat			指定のアニメパターン
 * @param   x			x座標
 * @param   y			y座標
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_SetLightPos( PHCLINK_WORK *wk, int lightno, int pat, int x, int y )
{
	if(y<0){
		y = 208+y;
		CATS_ObjectPosSetCap_SubSurface( wk->cap[PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+lightno],
										 x, y, PHCLINK_SUB_ACTOR_DISTANCE );
		CATS_ObjectAnimeSeqSetCap(wk->cap[PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+lightno], 4+pat);
		CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+lightno ], ACT_ON );
		CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+lightno ], ACT_OFF );

	}else{
		CATS_ObjectPosSetCap( wk->cap[PHCLINK_OBJ_MAX+lightno],  x, y );
		CATS_ObjectAnimeSeqSetCap(wk->cap[PHCLINK_OBJ_MAX+lightno], 19+pat);
		CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+lightno ], ACT_OFF );
		CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+lightno ], ACT_ON );

	}

}


//==============================================================================
/**
 * @brief   「光」を生成
 *
 * @param   wk		PHC画面ワーク
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_LightObjBirth( PHCLINK_WORK *wk )
{
	int empty = _light_obj_search(wk->light);
	if(empty>=0){
		PHC_LIGHT_WORK *light = &wk->light[empty];
		light->use   = 1;
		light->speed = gf_mtRand()%6+2;
		light->rad   = gf_mtRand()%300;
		light->x     = 128;
		light->y     = 116;
		light->move  = gf_mtRand()%40+40;
		light->time  = gf_mtRand()%60+120;
	}
}


//==============================================================================
/**
 * @brief   「光」を生成
 *
 * @param   wk		PHC画面ワーク
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_DownLightObjBirth( PHCLINK_WORK *wk )
{
	int empty = _light_obj_search(wk->light);
	if(empty>=0){
		PHC_LIGHT_WORK *light = &wk->light[empty];
		light->use   = 1;
		light->speed = -1*((gf_mtRand()%5)+5);
		light->rad   = gf_mtRand()%300;
		light->x     = 128;
		light->y     = -109;
		light->move  = gf_mtRand()%40+40;
		light->time  = gf_mtRand()%60+120;
	}
}


//=============================================================================================
/**
 * @brief	 光を早死にさせて0になるのを検出する
 *
 * @param   wk		PHC画面ワーク
 *
 * @retval  int		0:光は無い	1以上：光の数
 */
//=============================================================================================
int PhcLinkObj_LightEarlyDie( PHCLINK_WORK *wk )
{
	int i,count=0;
	for(i=0;i<PHC_LIGHT_MAX;i++){
		PHC_LIGHT_WORK *light = &wk->light[i];
		if(light->use){
			light->time-=20;
			count++;
		}
	}

	return count;
}

//==============================================================================
/**
 * @brief   「光」の挙動エンジン
 *
 * @param   wk		PHC画面ワーク
 *
 * @retval  none
 */
//==============================================================================
void PhcLinkObj_LightObjMain( PHCLINK_WORK *wk )
{
	int i,r;

	// 光の玉の挙動を制御する
	for(i=0;i<PHC_LIGHT_MAX;i++){
		PHC_LIGHT_WORK *light = &wk->light[i];
		int yoko = light->move*(180+light->y)/320;
		if(light->use){
			light->dy = Sin360R(light->rad)/360;
			light->dx = yoko*Cos360R(light->rad)/0x1000;
			light->rad+=10;

			PhcLinkObj_SetLightPos( wk, i, ((light->rad)%360)/73, light->x+light->dx, light->y+light->dy);
			light->y-=light->speed;

			// 上に消えた＆生存時間を過ぎたら場合は表示を一旦OFF
			light->time--;
			if(light->y<-110 || light->time<0 || light->y>160){
				light->use = 0;
				CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+i ], ACT_OFF );
				CATS_ObjectEnableCap( wk->cap[ PHCLINK_OBJ_MAX+i ], ACT_OFF );
			}

			r = light->rad%360;
			if(r>=0 && r<=180){
				CATS_ObjectPriSetCap( wk->cap[ PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+i ], 1);
				CATS_ObjectPriSetCap( wk->cap[ PHCLINK_OBJ_MAX+i ], 1);
			}else{
				CATS_ObjectPriSetCap( wk->cap[ PHCLINK_OBJ_MAX+PHC_LIGHT_MAX+i ], 3);
				CATS_ObjectPriSetCap( wk->cap[ PHCLINK_OBJ_MAX+i ], 3);

			}
		}
	}
//	OS_Printf("light x = %d, y=%d\n", wk->light[i].dx, wk->light[i].dy);
}


//=============================================================================================
/**
 * @brief	 光OBJワーク初期化
 *
 * @param   wk		PHC画面ワーク
 */
//=============================================================================================
void PhcLink_LightObjInit( PHCLINK_WORK *wk )
{
	int i;
	for(i=0;i<PHC_LIGHT_MAX;i++){
		PHC_LIGHT_WORK *light = &wk->light[i];
		light->use  = 0;
		light->time = 0;
	}
}

//------------------------------------------------------------------
/**
 * @brief   空いているlightOBJを探す
 *
 * @param   lights	PHC画面ワーク
 *
 * @retval  int		空いていたOBJ管理場所番号
 */
//------------------------------------------------------------------
static int _light_obj_search( PHC_LIGHT_WORK *lights )
{
	int i;
	for(i=0;i<PHC_LIGHT_MAX;i++){
		if(lights[i].use==0){
			return i;
		}
	}
	return -1;
}






//=============================================================================================
/**
 * @brief	 ポケモンの小さくなった光を表示ＯＮ
 *
 * @param   wk
 */
//=============================================================================================
void PhcLinkObj_PokeLumpAppear( PHCLINK_WORK *wk, PHC_LIGHT_WORK *light )
{
	if(light->y<0){
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_S ], ACT_ON );
	}else{
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_M ], ACT_ON );
	}
	CATS_ObjectAutoAnimeSetCap(  wk->cap[PLOBJ_ID_LIGHT_POKE_S], CATS_ANM_AUTO_ON);
	CATS_ObjectAutoAnimeSetCap(  wk->cap[PLOBJ_ID_LIGHT_POKE_M], CATS_ANM_AUTO_ON);
	CATS_ObjectAnimeSeqSetCap(wk->cap[PLOBJ_ID_LIGHT_POKE_S], 10);
	CATS_ObjectAnimeSeqSetCap(wk->cap[PLOBJ_ID_LIGHT_POKE_M], 24 );

}

//=============================================================================================
/**
 * @brief	 ポケモンの小さくなった光を表示ＯＦＦ
 *
 * @param   wk
 */
//=============================================================================================
void PhcLinkObj_PokeLumpDisappear( PHCLINK_WORK *wk )
{
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_S ], ACT_OFF );
	CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_M ], ACT_OFF );

}

//=============================================================================================
/**
 * @brief	 ポケモンの小さくなった光の上下画面管理
 *
 * @param   wk
 * @param   x
 * @param   y
 */
//=============================================================================================
void PhcLinkObj_PokeLumpSetPos( PHCLINK_WORK *wk, int x, int y )
{

	// 上下画面の行き来を意識して表示ON/OFF、座標設定を行う
	if(y<0){
		// 上画面
		y = 208+y;
		CATS_ObjectPosSetCap_SubSurface( wk->cap[ PLOBJ_ID_LIGHT_POKE_S], x, y, PHCLINK_SUB_ACTOR_DISTANCE );
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_S ], ACT_ON );
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_M ], ACT_OFF );

	}else{
		// 下画面
		CATS_ObjectPosSetCap( wk->cap[PLOBJ_ID_LIGHT_POKE_M],  x, y );
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_M ], ACT_ON );

		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_LIGHT_POKE_S ], ACT_OFF );
	}

}

//=============================================================================================
/**
 * @brief	 エフェクト用にポケモン光を初期化する
 *
 * @param   wk
 * @param   light	管理ワーク（光の使いまわし）
 * @param   flag	0:上る時	1:下る時  2:降りて来て留まる動き
 */
//=============================================================================================
void PhcLinkObj_PokeLumpInit( PHCLINK_WORK *wk, PHC_LIGHT_WORK *light, int flag )
{
	if(flag==0){
		light->x    = 128;
		light->y    = 130;
		light->move = -10;
		light->time = 0;
	}else if(flag==1){
		light->x    = 128;
		light->y    = -110;
		light->move = 10;
		light->time = 0;

	}else if(flag==2){
		light->x    = 128;
		light->y    = 130;
		light->move = 0;
		light->time = 0;
	}
	light->dx = 0;
	light->dy = 0;

	PhcLinkObj_PokeLumpAppear( wk, light );

}

//=============================================================================================
/**
 * @brief	 ポケモン光が飛んでいくアニメ
 *
 * @param   wk
 * @param   light
 *
 * @retval  int		0:再生中	1:終わった
 */
//=============================================================================================
int PhcLinkObj_PokeLumpMain( PHCLINK_WORK *wk, PHC_LIGHT_WORK *light )
{
	// 上る時
	if(light->move<0){
		light->y += light->move;
		if(light->y < -110){
			light->move = 0;
		}
		PhcLinkObj_PokeLumpSetPos( wk, light->x, light->y );
		return 0;

	// 下る時
	}else if(light->move>0 && light->move<=100){
		light->y += light->move;
		if(light->y > 108){
			light->move = 101;
		}
		PhcLinkObj_PokeLumpSetPos( wk, light->x, light->y );
		return 0;
	}else if(light->move!=0){
		// 降りて来た後、その場に留まる(外部から消される）
		PhcLinkObj_PokeLumpSetPos( wk, light->x, light->y );
		return 0;
	}

	// moveが0になったら消去
	PhcLinkObj_PokeLumpDisappear( wk );

	return 1;
}


#define POKEICON_VRAM_OFFSET ( 26*7+10  )
#define POKEICON_TRANS_CELL  ( 4*4*2      )
#define POKEICON_TRANS_SIZE	 ( POKEICON_TRANS_CELL*0x20 )
#define POKEICON_PAL_OFFSET	 ( 5 )


//----------------------------------------------------------------------------------
/**
 * @brief	 ポケモンアイコンパレット転送
 *
 * @param   none
 */
//----------------------------------------------------------------------------------
static void pokeicon_pal_trans( void )
{
	// ポケモンアイコン用パレットを一気に読み込んでVRAM転送する
	ArcUtil_PalSet( ARC_POKEICON, PokeIconPalArcIndexGet(), PALTYPE_MAIN_OBJ,
					POKEICON_PAL_OFFSET*0x20, 0x20*3, HEAPID_PHC_LINK );
}

//=============================================================================================
/**
 * @brief	 ポケモンアイコン画像読み込み＆転送
 *
 * @param   wk
 * @param   pos
 * @param   monsno
 * @param   form
 */
//=============================================================================================
void PhcLinkObj_PokeIconChange( PHCLINK_WORK *wk, int pos, int monsno, int form )
{

	wk->PokeIconBuf = ArcUtil_CharDataGet( ARC_POKEICON,
					PokeIconCgxArcIndexGetByMonsNumber( monsno,
														0, 	// タマゴはない
														form ),
					0, 	&wk->PokeIconCharaDat,
		HEAPID_PHC_LINK );
	DC_FlushRange( wk->PokeIconCharaDat->pRawData, POKEICON_TRANS_SIZE );
	TransPokeIconCharaPal(wk->PokeIconCharaDat,
						  monsno, form, 0,
						  pos, wk->cap[ PLOBJ_ID_POKEICON_0+pos]);

	sys_FreeMemoryEz( wk->PokeIconBuf );
}
//------------------------------------------------------------------
/**
 * @brief	 ポケモンアイコンを転送する（ポケモンの名前の時だけ）
 *
 * @param   chara	キャラクターファイルアドレス
 * @param   pal		パレットファイルアドレス
 * @param   pokeno	ポケモンの開発NO
 *
 * @retval  none
 */
//------------------------------------------------------------------
static void TransPokeIconCharaPal( NNSG2dCharacterData *chara, int pokeno, int form, int tamago, int no, CATS_ACT_PTR icon)
{
	u8 *pokepal;

	// キャラクター転送
	GX_LoadOBJ(chara->pRawData, (POKEICON_VRAM_OFFSET+no*POKEICON_TRANS_CELL)*0x20, POKEICON_TRANS_SIZE);
	OS_Printf("アイコンキャラ転送NO. %d\n",no);


	// パレット設定
//	CLACT_PaletteOffsetChg( icon,  PokeIconPalNumGet( pokeno, form, tamago )+POKEICON_PAL_OFFSET);
	CATS_ObjectPaletteSetCap( icon ,  PokeIconPalNumGet( pokeno, form, tamago )+POKEICON_PAL_OFFSET);

}

